package org.sky.framework.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CsvUtils {
	private static final Logger log = LoggerFactory.getLogger(CsvUtils.class);
	static String charset = "UTF-8";

	/**
	 * 写csv文件
	 * 
	 * @param fileName
	 * @param datas
	 * @return
	 */
	public static File writeCsvFile(String fileName, List<List<String>> datas) {
		File csvFile = null;
		BufferedWriter csvFileOutputStream = null;
		CSVPrinter csvPrinter = null;
		try {
			csvFile = new File(fileName);
			File file = new File(csvFile.getParent());
			if (!file.exists()) {
				file.mkdir();
			}
			csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), charset),
					1024);
			// 初始化 CSVPrinter
			List<String> header = datas.get(0);
			String[] headers = new String[header.size()];
			header.toArray(headers);
			CSVFormat csvFileFormat = CSVFormat.DEFAULT;
			csvPrinter = new CSVPrinter(csvFileOutputStream, csvFileFormat);
			List<String> ls = null;
			if (datas != null) {
				for (int i = 0; i < datas.size(); i++) {
					ls = new ArrayList<String>();
					ls = datas.get(i);
					for (int j = 0; j < ls.size(); j++) {
						csvPrinter.print(ls.get(j));
					}
					csvPrinter.println();// 换行
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("csv文件写入异常");
		} finally {
			try {
				csvFileOutputStream.flush();
				csvFileOutputStream.close();
				csvPrinter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return csvFile;
	}

	/**
	 * 读取csv文件
	 * 
	 * @param file
	 * @return
	 */
	public static List<List<String>> readCsvFile(String file) {
		BufferedReader br = null;
		CSVParser csvFileParser = null;
		List<List<String>> rows = null;
		CSVFormat csvFileFormat = CSVFormat.DEFAULT;
		try {
			// 初始化FileReader object
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));// 解决乱码问题
			// 初始化 CSVParser object
			csvFileParser = new CSVParser(br, csvFileFormat);
			// CSV文件records
			List<CSVRecord> csvRecords = csvFileParser.getRecords();
			rows = new ArrayList<List<String>>();
			for (int i = 0; i < csvRecords.size(); i++) {
				CSVRecord record = csvRecords.get(i);
				Iterator<String> iterator = record.iterator();
				List<String> cells = new ArrayList<String>();
				while (iterator.hasNext()) {
					cells.add(iterator.next());
				}
				rows.add(cells);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("csv文件读取异常");
		} finally {
			try {
				br.close();
				csvFileParser.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rows;
	}
}
